﻿using Dapper;
using ERPEF.Model;
using ERPIService;
using ERPService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using S4Project.Model;
using SkiaSharp;
using System.Data;

namespace S4Project.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
        public class QuanXianController : ControllerBase
        {
            private readonly QuanXianService quanXianService;
            private readonly MyDbContext myDbContext;
            public QuanXianController(MyDbContext myDb, QuanXianService quanXianService)
            {
                myDbContext = myDb;
                this.quanXianService = quanXianService;
            }

            //查询
            [HttpGet]
            public async Task<IActionResult> Query()
            {
            var users = await quanXianService.Query();
            return Ok(users);
        }
       
        // 获取所有角色
        [HttpGet]
        public async Task<IActionResult> GetAllRoles()
        {
            var roles = await quanXianService.GetAllRoles();
            return Ok(roles);
        }

     
        [HttpPost]
        public async Task<IActionResult> CreateUserWithRoles([FromBody] CreateUserWithRolesRequest request)
        {
            var result = await quanXianService.CreateUserWithRolesAsync(request.User, request.RoleIds);
            if (result)
            {
                return Ok(new { message = "用户和角色创建成功" });
            }
            else
            {
                return BadRequest(new { message = "用户或角色创建失败" });
            }
        }

        public class CreateUserWithRolesRequest
        {
            public User User { get; set; }
            public List<int> RoleIds { get; set; }
        }


        [HttpPut]
        public async Task<IActionResult> UpdateUserRoles([FromBody] UpdateUserRolesRequest request)
        {
            var result = await quanXianService.UpdateUserRolesAsync(request.UserId, request.RoleIds);
            if (result)
            {
                return Ok(new { message = "用户角色更新成功" });
            }
            else
            {
                return BadRequest(new { message = "用户角色更新失败" });
            }
        }
        public class UpdateUserRolesRequest
        {
            public int UserId { get; set; }
            public List<int> RoleIds { get; set; }
        }


        [HttpDelete]
        public async Task<IActionResult> DeleteUser(int id)
        {
            var result = await quanXianService.DeleteUserWithRolesAsync(id);
            if (result)
            {
                return Ok(new { message = "用户及其角色删除成功" });
            }
            else
            {
                return BadRequest(new { message = "用户或角色删除失败" });
            }
        }






        [HttpGet]
        public async Task<List<rolesuser>> UserSelects()
        {
            return await quanXianService.UserSelects();
        }
        [HttpGet]
        public async Task<List<Trees>> TressSelect()
        {
            return await quanXianService.TreesSelects();
        }
        [HttpGet]
        public async Task<int[]> QianBian(int pid)
        {
            return await quanXianService.GetQid(pid);
        }
        //新增修改权限
        [HttpPost]
        public async Task<bool> addquan(int[] c, int pid)
        {
            return await quanXianService.Addquan(c, pid);
        }
        [HttpGet]
        public async Task<Roles> Rbac_Rolesselectc()
        {
            return await quanXianService.rbac_Roles();

        }
        [HttpPost]
        public async Task<bool> addrole(Roles roles)
        {
            return await quanXianService.addroid(roles);
        }
        [HttpDelete]
        public async Task<bool> deleterblo(int roid)
        {
            return await quanXianService.deleterblo(roid);
        }


        [HttpGet]
        public async Task<ActionResult<List<CeBianlan>>> CeBianlanSelect(int JuriID)
        {
           return await quanXianService.CeBianlanSelectAsync(JuriID);
        }

        [HttpGet]
        public async Task<ActionResult<UserRoles>> Rbac_UserRoles(int RolesID)
        {
            return await quanXianService.queryU_rolesiD(RolesID);
        }
        /// <summary>
        /// 分页查询 
        /// </summary>
        /// <returns></returns> 
        /// 
        [HttpGet]
        public ActionResult<FenYe<Roles>> getFenYe(int currentPage, int pageSize, string where)
        {
            using (var conn = myDbContext.Database.GetDbConnection())
            {
                conn.Open();
                var parameters = new DynamicParameters();
                parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: pageSize);
                parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "RolesID"); // 更改为正确的列名
                parameters.Add("tableName", dbType: DbType.String, direction: ParameterDirection.Input, value: "Roles"); // 更改为正确的表名
                parameters.Add("currentPage", dbType: DbType.Int32, direction: ParameterDirection.Input, value: currentPage);
                parameters.Add("where", dbType: DbType.String, direction: ParameterDirection.Input, value: where); // 添加 where 参数
                parameters.Add("row", dbType: DbType.Int32, direction: ParameterDirection.Output);  // 修改为和存储过程中一致的参数名
                string sql = "[dbo].[FenYe]";
                var result = conn.Query<Roles>(sql, parameters, commandType: CommandType.StoredProcedure);
                int row = parameters.Get<int>("row");
                FenYe<Roles> fenYe = new FenYe<Roles>()
                {
                    RolesData = result,
                    Rows = row,
                };

                if (fenYe.RolesData.Any())
                {
                    return fenYe;
                }
                else
                {
                    return Content("没有查到数据");
                }
            }
        }
    }
}
